<?php
/**
 * Created by PhpStorm.
 * User: nhson219
 * Date: 9/10/14
 * Time: 10:56 AM
 */
?>
<form id="frm-create-milestone" class="form-milestone" method="POST">
    <div class="col-lg-12 col-md-12 col-sm-12 no-padding">
        <h1><?php echo $this->lang->line('Select Milestone');?></h1>
<!--            <input type="hidden" name="--><?php //echo $this->security->get_csrf_token_name(); ?><!--" value="--><?php //echo $this->security->get_csrf_hash(); ?><!--" />-->
        <div class="form-group">
                <label class="col-sm-5 control-label" for="exampleInputEmail1"><?php echo $this->lang->line('Milestone Category')?></label>
                <div class="col-lg-5 col-md-5 col-sm-5">
                    <select onchange="loadMilestone()" id="cate-milestone" name="milestone_cateId" class="form-control">
                        <option value="" selected><?php echo $this->lang->line('Select Milestone');?></option>
                        <?php
                            if($cate_milestones->num_rows() > 0)
                            {
                                foreach($cate_milestones->result() as $item){
                        ?>
                        <option  value="<?php echo $item->id?>"><?php echo $item->title?></option>
                        <?php } } ?>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-5 control-label" for="exampleInputEmail1">Tổng Milestone : <?php echo $sum_bid;?></label>
            </div>
                <span style="display: none" id="project_id"><?php echo $project->id;?></span>
            <div id="error-milestone" class="col-lg-12 error">

            </div>
            <div class="col-lg-12 col-md-12 col-sm-12" id="item-milestone">
            </div>
            <div id="save-milestone" style="display: none">
                <div class="col-lg-12" style="text-align: center;">
                    <div class="col-lg-7"><a class="btn-similar-project btn-complete">
                            <?php echo $this->lang->line('Complete');?>
                    </a>
                    </div>
                </div>
                <div style="text-align: center;" class="col-lg-12">
                    <button style="margin:0 auto;" type="button" onclick="createCustomMilestone()" class="btn-similar-project btn-complete">
                        <?php echo $this->lang->line('Save');?>
                    </button>
                </div>
            </div>
            <div class="col-lg-12 col-md-12 col-sm-12" id="btn-create-milestone" style="display: none;">
                <button type="button" class="btn-similar-project"  ><?php echo $this->lang->line('Create Milestone');?></button>
            </div>
    </div>
</form>
<script>
    $(document).ready(function(){
        loadMilestone();
    });
    function loadMilestone(){
        $.ajax({
            type: "POST",
            url: "<?php echo base_url()."index.php/project/loadMilestone"?>",
            data: "value="+$('#cate-milestone').val(),
            success: function (data) {
                data = $.parseJSON(data);
                if (data.status == 'error') {
                    //console.log(data.message);

                } else {
                    var mil = $('#item-milestone');
                    mil.empty();
                    $.each(data.data,function(k){
                        $('<div style="text-align: center;" class="col-lg-12"> <div class="col-lg-7"><input type="text" class="a" disabled value="'+data.data[k].title+'"></div><span class="percent-milestone col-lg-1">'+data.data[k].return_fee+'%</span>'+'<span class="glyphicon glyphicon-arrow-down col-lg-7"></span></div>').appendTo(mil);
                        if((data.data.length - 1) == k){
                            $('<div style="text-align: center;" class="col-lg-12"> <div class="col-lg-7"><a class="btn-similar-project btn-complete">'+'<?php echo $this->lang->line('Complete')?>'+'</a></div></div>').appendTo(mil);
                            $('<div style="text-align: center;" class="col-lg-12"><button onclick="createMilestone()" style="margin:0 auto;" type="button"  class="btn-similar-project btn-complete">'+'<?php echo $this->lang->line('Save');?>'+'</button></div>').appendTo(mil);
                        }
                    });
                }
            }
        });
    }
    function createMilestone(){
        var data = $("#frm-create-milestone").serializeArray();
        data.push({"name" : "project_id", "value" :  $("#project_id").text() });
        $.ajax({
            type: "POST",
            url: "<?php echo base_url()."index.php/project/createMilestone"?>",
            data: data,
            success: function (data) {
                data = $.parseJSON(data);
                if (data.status == 'error') {
                    $('#error-milestone').html(data.message);
                } else {
                    $(".bid").load('<?php echo base_url()."index.php/project/editMilestone"?>',{'project_id':<?php echo $project->id;?>});
                }
            }
        });
    }
    $("#btn-create-milestone").click(function(){
        $("#save-milestone").show();
        var mil = $("#item-milestone");
        $('<div class="col-lg-12"  style="text-align: center;"> <div class="col-lg-7"><input required type="text" name="name[]" value="" placeholder="'+'<?php echo $this->lang->line('Fill Name Milestone')?>'+'"></div><div class="col-lg-2"><input type="text" name="fee[]" value=""  /><span>USD</span></div><div style="padding-top:10px;" class="col-lg-1"><button type="button" id="btn-remove-milestone" onclick="RemoveItemMilestone()" class="btn-similar-project">'+'<?php echo $this->lang->line('Delete')?>'+'</button></div> <span class="glyphicon glyphicon-arrow-down col-lg-7"></span></div>').appendTo(mil);
    });

    function createCustomMilestone(){
        var data = JSON.stringify($("input[name='name[]']").serializeArray());
        var fee = JSON.stringify($("input[name='fee[]']").serializeArray());
        $.ajax({
            type: "POST",
            url: "<?php echo base_url()."index.php/project/createCustomMilestone"?>",
            data: {data : data,fee : fee,projects_id : $("#project_id").text()},
            beforeSend: function(){

            },
            success: function (data) {
                data = $.parseJSON(data);
                if (data.status == 'error') {
                    $('#error-milestone').html(data.message);
                } else {
                    //$(".bid").load('<?php echo base_url()."index.php/project/editMilestone"?>',{'project_id':<?php echo $project->id;?>});
                    window.location = '<?php echo site_url('project/view/'.$project->id)?>'
                }
            }
        });
    }

</script>
